#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int answer[N],n,m,A[N],T,num_ans=0;
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&T);
    while(T--){
        memset(A,0,sizeof(A));
        bool B[N];
        bool exist[N];
        memset(exist,false,sizeof(exist));
        memset(B,false,sizeof(B));
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&A[i]);
        }
        exist[A[1]]=true;int ans=0;
        for(int i=2;i<=n;i++){
            if(!exist[A[i]]){
                    B[A[i]]=B[A[i-1]];
                    exist[A[i]]=true;
            }
            else {
                int sum=0;
                B[A[i]]!=B[A[i-1]];
                for(int j=i;j>1;j--){
                    for(int k=j-1;k>=1;k--){
                        if(B[A[j]]==B[A[k]]){
                            sum+=A[j];
                            break;
                        }
                    }
                }
                if(sum>ans)ans=sum;
                else B[A[i]]=B[A[i-1]];
            }
        }
        answer[++num_ans]=ans;
    }
    for(int i=1;i<=num_ans;i++){
        cout<<answer[i]<<endl;
    }
}
